#include <STC15F2K60S2.H>
#include <init.h>
#include <Seg.h>
#include <Led.h>
#include <Key.h>
#include <Ds1302.h>
#include <iic.h>
unsigned char ucLed[]={0,0,0,0,0,0,0,0};
unsigned long int uwTick;
unsigned char Seg_Pos;
unsigned char Seg_Buf[]={10,10,10,10,10,10,10,10};
unsigned char Key_Val,Key_Down,Key_Up,Key_Old;
unsigned char ucRtc[]={23,59,55};
unsigned int AD_in_100x1;//笔记思路
unsigned int AD_in_100x2;//笔记思路
unsigned int AD_in_100x1_Old;//笔记思路
unsigned int AD_in_100x2_Old;//笔记思路

unsigned char Seg_Mode;
bit chufa;
unsigned int time_3000ms;
unsigned char chufa_history[3][3];
unsigned char index;
unsigned char show_index;//笔记思路，数组索引
unsigned char index_num=1;
unsigned char chufa_num;

void Led_Proc()
{
	if(chufa==0)
	{
		ucLed[0]=(Seg_Mode==0)?1:0;
		ucLed[1]=(Seg_Mode==1)?1:0;
		ucLed[2]=(Seg_Mode==2)?1:0;
		ucLed[7]=1;
	}
	else
	{
		ucLed[0]=0;
		ucLed[1]=0;
		ucLed[2]=0;
		ucLed[7]=0;
	}
	Led_Disp(ucLed);
}

void Key_Proc()
{
	Key_Val=Key_Read();
	Key_Down=Key_Val&(Key_Val^Key_Old);
	Key_Up=Key_Val&(Key_Val^Key_Old);
	Key_Old=Key_Val;
	
	switch(Key_Down)
	{
		case 8:
			if(chufa)return;
			if(++Seg_Mode==3)Seg_Mode=0;
			if(Seg_Mode==2)index_num=1;
		break;
		case 9:
			if(--show_index==255)show_index=2;
			if(++index_num==4)index_num=1;
		break;
		case 12:
			if(Seg_Mode==2&&chufa==0)
				chufa_num=0;
		break;
		
	}
	
	
}

void Ds1302_Read()
{
	Read_Rtc(ucRtc);
}

void Seg_Proc()
{
	
	if(chufa)
	{
		Seg_Buf[0]=15;
		Seg_Buf[1]=15;
		Seg_Buf[2]=chufa_history[index][0]/10;
		Seg_Buf[3]=chufa_history[index][0]%10;
		Seg_Buf[4]=chufa_history[index][1]/10;
		Seg_Buf[5]=chufa_history[index][1]%10;
		Seg_Buf[6]=chufa_history[index][2]/10;
		Seg_Buf[7]=chufa_history[index][2]%10;
		
	}
	else
	{
		switch(Seg_Mode)
	{
		case 0:
			Seg_Buf[0]=ucRtc[0]/10;
			Seg_Buf[1]=ucRtc[0]%10;
			Seg_Buf[3]=ucRtc[1]/10;
			Seg_Buf[4]=ucRtc[1]%10;
			Seg_Buf[6]=ucRtc[2]/10;
			Seg_Buf[7]=ucRtc[2]%10;
			Seg_Buf[2]=12;
			Seg_Buf[5]=12;
		break;
		case 1:
			Seg_Buf[0]=13;
			Seg_Buf[1]=AD_in_100x1/100+',';
			Seg_Buf[2]=AD_in_100x1/10%10;
			Seg_Buf[3]=AD_in_100x1%10;
			Seg_Buf[4]=14;
			Seg_Buf[5]=AD_in_100x2/100+',';
			Seg_Buf[6]=AD_in_100x2/10%10;
			Seg_Buf[7]=AD_in_100x2%10;
		break;
		case 2:
			if(chufa_num>=index_num)
			{
				Seg_Buf[0]=11;
				Seg_Buf[1]=index_num;
				Seg_Buf[2]=chufa_history[show_index][0]/10;
				Seg_Buf[3]=chufa_history[show_index][0]%10;
				Seg_Buf[4]=chufa_history[show_index][1]/10;
				Seg_Buf[5]=chufa_history[show_index][1]%10;
				Seg_Buf[6]=chufa_history[show_index][2]/10;
				Seg_Buf[7]=chufa_history[show_index][2]%10;
			}
			else
			{
				Seg_Buf[0]=11;
				Seg_Buf[1]=index_num;
				Seg_Buf[2]=12;
				Seg_Buf[3]=12;
				Seg_Buf[4]=12;
				Seg_Buf[5]=12;
				Seg_Buf[6]=12;
				Seg_Buf[7]=12;
			}
		break;
	}
	}
	
	
	
}

void AD_Read()
{
	unsigned char i;
	AD_in_100x1=Ad_Read(0x43)*100/51;//先乘法后除法
	AD_in_100x2=Ad_Read(0x41)*100/51;//先乘法后除法
	if(AD_in_100x1_Old>=AD_in_100x2_Old)
	{
		if(chufa==0)
		{
			if(AD_in_100x1<AD_in_100x2)
			{
				chufa=1;
				for(i=0;i<3;i++)
				{
					chufa_history[index][i]=ucRtc[i];
				}
				show_index=index;
				chufa_num++;
				
				
			}
		}
	}
	AD_in_100x1_Old=AD_in_100x1;
	AD_in_100x2_Old=AD_in_100x2;
}

void Timer1_Server()interrupt 3
{
	uwTick++;         // 系统计时增加
	Seg_Pos = (++Seg_Pos) % 8; // 更新数码管位置,比原模版更省时间
	 // 数码管显示处理
    if (Seg_Buf[Seg_Pos] > 20)
    Seg_Disp(Seg_Pos, Seg_Buf[Seg_Pos] - ',', 1); // 带小数点
    else
    Seg_Disp(Seg_Pos, Seg_Buf[Seg_Pos], 0); // 无小数点

	if(chufa)
	{
		if(++time_3000ms==3000)
		{
			time_3000ms=0;
			chufa=0;
			if(++index==3)index=0;
			index_num=1;
		}
	}
	
}

void Timer1_Init(void)
{
  AUXR &= 0xBF; // 定时器时钟12T模式
  TMOD &= 0x0F; // 设置定时器模式
  TL1 = 0x18;   // 设置定时初始值
  TH1 = 0xFC;   // 设置定时初始值
  TF1 = 0;      // 清除TF1标志
  TR1 = 1;      // 启动定时器1
  ET1 = 1;      // 使能定时器1中断
  EA = 1;       // 使能总中断
}
/* 调度器任务结构体定义 */
typedef struct
{
  void (*task_func)(void);    // 任务函数
  unsigned long int rate_ms;  // 任务执行周期（毫秒）
  unsigned long int last_run; // 任务上次运行时间
} task_t;

/* 调度器任务列表 */
idata task_t Scheduler_Task[] = {
    {Led_Proc, 1, 0},        // LED任务，每1毫秒执行一次
	{Key_Proc,10,0},
	{Ds1302_Read,300,0},
	{Seg_Proc,150,0},
	{AD_Read,210,0},//ADC转换后读的是上一次的数据
    
};

idata unsigned char task_num;//任务数量

/* 调度器初始化 */
void Scheduler_Init(void)
{
  task_num = sizeof(Scheduler_Task) / sizeof(task_t); // 计算任务数量
}

/* 调度器运行 */
void Scheduler_Run(void)
{
 
  unsigned char i;
  for (i = 0; i < task_num; i++)
  {
    unsigned long int now_time = uwTick; // 获取当前时间
    if (now_time >= (Scheduler_Task[i].last_run + Scheduler_Task[i].rate_ms))
    {
      Scheduler_Task[i].last_run = now_time; // 更新任务上次运行时间
      Scheduler_Task[i].task_func();         // 执行任务
    }
  }
}

void main()
{
	System_Init();
	Timer1_Init();
	Scheduler_Init();
	Set_Rtc(ucRtc);
	while(1)
	{
		Scheduler_Run();
	}
}

